package com.example.demo.controller;

import com.example.demo.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@CrossOrigin
@RestController
@RequestMapping("/chat")
public class ChatController {

    @Autowired
    private ChatService chatService;

    // 接收用户消息，并返回 AI 的响应
    @PostMapping("/send")
    public Map<String, String> sendMessage(@RequestParam String query) {
        Map<String, String> response = new HashMap<>();
        try {
            String aiResponse = chatService.sendMessage(query, "1c89bd20-09a3-4f4c-b942-37e726bed75a");
            response.put("answer", aiResponse);  // 返回的 key 为 "answer"
        } catch (IOException e) {
            response.put("answer", "Error: " + e.getMessage());
        }
        return response;  // Spring Boot 会自动将 Map 转换为 JSON
    }

}